//
//  SpiralOrder.swift
//  LeetCodeSummary
//
//  Created by 彭西西 on 2020/7/22.
//  Copyright © 2020 WangYonghe. All rights reserved.
//  剑指 Offer 29. 顺时针打印矩阵

import UIKit

/*
 剑指 Offer 29. 顺时针打印矩阵

 输入一个矩阵，按照从外向里以顺时针的顺序依次打印出每一个数字。

  

 示例 1：

 输入：matrix = [[1,2,3],[4,5,6],[7,8,9]]
 输出：[1,2,3,6,9,8,7,4,5]
 示例 2：

 输入：matrix = [[1,2,3,4],[5,6,7,8],[9,10,11,12]]
 输出：[1,2,3,4,8,12,11,10,9,5,6,7]
  

 限制：

 0 <= matrix.length <= 100
 0 <= matrix[i].length <= 100
 注意：本题与主站 54 题相同：https://leetcode-cn.com/problems/spiral-matrix/
 */

class SpiralOrder: NSObject {
    
    //暴力循环法 不能通过全部的用例  需要完善
    func spiralOrder(_ matrix: [[Int]]) -> [Int] {
        if matrix.count == 0 {
            return []
        }
        if matrix.count == 1 {
            return matrix[0]
        }
        
        var resArr = [Int]()
        
        
        var a = 0
        
        while true {
            if a > matrix[0].count-1 || resArr.count == matrix.count * matrix[0].count {
                break
            }
            //上边界 a ... matrix[0].count - 1
            for i in a ... matrix[0].count-1-a{
                resArr.append(matrix[a][i])
            }
            if matrix.count-1-a == a {
                break
            }
            print("\(resArr)")
            
            //右边 a+1 ... matrix.count - 1
            for j in a ... matrix.count-1-a{
                if j == a {
                    continue
                }
                resArr.append(matrix[a+j][matrix[0].count-1-a])
            }
            if matrix[0].count-a == a {
                break
            }
            print("\(resArr)")
            
            //下边 == 上边界 反向
            for k in (a ... matrix[0].count-1-a).reversed(){
                if k == matrix[0].count-1-a {
                    continue
                }
                resArr.append(matrix[matrix.count-1-a][k])
            }
            if matrix.count-1-a == a{
                break
            }
            print("\(resArr)")
            
            if resArr.count == matrix.count * matrix[0].count {
                break
            }
            //左边 == 右边反向
            for l in (a ... matrix.count-1-a-1).reversed(){
                if l == a{
                    a += 1
                    continue
                }
                resArr.append(matrix[l][a])
            }
            print("\(resArr)")
            
        }

        return resArr
    }
    
    
    
    //正确的解法
    func spiralOrder2(_ matrix: [[Int]]) -> [Int] {
        if matrix.count == 0 {
            return []
        }
        
        var resArr = [Int]()
        
        //左边界
        var left = 0
        
        //右边界
        var right = matrix[0].count - 1
        
        //上边界
        var up = 0
        
        //下边界
        var bottom = matrix.count - 1
        
        while true {
            //从左到右
            for i in left ... right{
                resArr.append(matrix[up][i])
            }
            up = up+1
            if up > bottom {
                break
            }
            
            //从上到下
            for i in up ... bottom{
                resArr.append(matrix[i][right])
            }
            right = right-1
            if left > right {
                break
            }
            
            
            //从右到左
            for i in (left ... right).reversed(){
                resArr.append(matrix[bottom][i])
            }
            bottom = bottom-1
            if up > bottom {
                break
            }
            
            //从下至上
            for i in (up ... bottom).reversed(){
                resArr.append(matrix[i][left])
            }
            left = left+1
            if left > right {
                break
            }
        }

        return resArr
    }
}
